package jesss;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import jesss.commands.CommandResult;
import jesss.commands.JesssCommand;
import jesss.commands.JesssCommandFactoryImpl;
import jesss.commands.ResultStatus;
import jesss.options.ExtendedGnuParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.MissingArgumentException;
import org.apache.commons.cli.Options;

import java.io.File;

/**
 * User: Lee
 * Date: 8/12/13
 * Time: 10:55 PM
 */
public class jesss {

    //private S3ManagerImpl s3Manager = null;

    private Options options = null;
    private String commandName = null;
    private boolean shouldDisplayVerbose = false;
    private String propertyFilePath = null;

    public static void main (String[] args) {

        try {
            jesss jesss = new jesss();
            jesss.parseArgs(args);

            AWSCredentials awsCredentials = jesss.makeAwsCredentials();
            AmazonS3 amazonS3 = new AmazonS3Client(awsCredentials);

            JesssCommand jc = JesssCommandFactoryImpl.getInstance().createCommand(amazonS3, jesss.commandName, jesss.shouldDisplayVerbose, args);
            CommandResult result = jc.executeCommand();

            if (result.getResultStatus().equals(ResultStatus.Error)) {
                System.err.println(result.getStatusMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public jesss() {
        try {
            setOptions();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    public AWSCredentials makeAwsCredentials() throws Exception {

        if (propertyFilePath == null) {
            throw new MissingArgumentException("Missing path to AWS credentials properties file.");
        }
        return new PropertiesCredentials(new File(propertyFilePath));
    }


    private void setOptions() {
        options = new Options();
        options.addOption("cn", "commandName", true, "Command name to execute");
        options.addOption("pf", "propertyFile", true, "Property file to use for credentials");
        options.addOption("v", "verbose", false, "Display verbose information");
    }

    public void parseArgs(String[] args) throws Exception {

        if (args == null || args.length == 0) {
            printUsage();
        }

        try {
            CommandLineParser parser = new ExtendedGnuParser(true);
            CommandLine cmd = parser.parse(options, args);

            if (cmd.hasOption("cn")) {
                commandName = cmd.getOptionValue("cn");
            } else {
                throw new MissingArgumentException("Missing command name parameter.");
            }

            if (cmd.hasOption("pf")) {
                propertyFilePath = cmd.getOptionValue("pf");
            } else {
                throw new MissingArgumentException("Missing properties file parameter.");
            }

            if (cmd.hasOption("v")) {
                shouldDisplayVerbose = true;
            }

        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            throw e;
        }

    }

    private void printUsage() {
        System.out.println("Usage:");
        System.out.println("jesss.jesss -cn <command name> -pf <properties file> [command parameters]");
    }
}
